<?php

namespace Base\NewsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
{

    public function indexAction()
    {
        $news_list = $this->getDoctrine()
                    ->getRepository('BaseNewsBundle:News')
                    ->getNews()
                    ->getArrayResult();

        /* Breadcrumbs */
        $breadcrumbs = $this->get( 'white_october_breadcrumbs' );
        $breadcrumbs->addItem( 'Accueil', $this->get("router")->generate( '_homepage' ));
        $breadcrumbs->addItem( 'Actualités' );
      
        return $this->render('BaseNewsBundle:Default:index.html.twig', array( 'news_list' => $news_list ));
    }

    public function showAction( $year, $month, $slug )
    {
        $news = $this->getDoctrine()
                    ->getRepository('BaseNewsBundle:News')
                    ->getCurrentNews($slug)
                    ->getSingleResult();

        /* Breadcrumbs */
        $breadcrumbs = $this->get( 'white_october_breadcrumbs' );
        $breadcrumbs->addItem( 'Accueil', $this->get("router")->generate( '_homepage' ));
        $breadcrumbs->addItem( 'Actualités', $this->get("router")->generate( '_baseNewsIndex' ));
        $breadcrumbs->addItem( $news[0]->title );

        return $this->render('BaseNewsBundle:Default:show.html.twig', array( 'news' => $news ));
    }

    public function homeAction()
    {
        $news_list = $this->getDoctrine()
                          ->getRepository('BaseNewsBundle:News')
                          ->findAllOrderedByUpdatedAt(11);

        $news_list_big_highlight = array();
        $news_list_small_highlight = array();
        $i = 0;

        foreach($news_list as $news) {
            if($i < 3) {
                $news_list_big_highlight[$i]['title'] = $news[ 'n_title' ];
                $news_list_big_highlight[$i]['slug'] = $news[ 'n_slug' ];
                $news_list_big_highlight[$i]['content'] = $news[ 'n_content' ];
                $news_list_big_highlight[$i]['author'] = $news[ 'u_username' ];
                $news_list_big_highlight[$i]['image'] = $i == 1 ? "http://www.cpfaizenay.com/media/actualites/2012-03/thumbnail/large_CPF_Aizenay_V2.png" : "http://www.cpfaizenay.com/media/actualites/2012-03/thumbnail/large_Journee4_jeune_PH2.jpg";
                $news_list_big_highlight[$i]['width'] = "707";
                $news_list_big_highlight[$i]['height'] = "618";
                $news_list_big_highlight[$i]['updatedAt'] = $news[ 'n_updatedAt' ]->format('d/m/Y');
                $news_list_big_highlight[$i]['year'] = $news[ 'n_updatedAt' ]->format('Y');
                $news_list_big_highlight[$i]['month'] = $news[ 'n_updatedAt' ]->format('m');
            }else{
                $index_small = $i-3;
                $news_list_small_highlight[$index_small]['title'] = $news[ 'n_title' ];
                $news_list_small_highlight[$index_small]['slug'] = $news[ 'n_slug' ];
                $news_list_small_highlight[$index_small]['updatedAt'] = $news[ 'n_updatedAt' ]->format('d/m/Y');
                $news_list_small_highlight[$index_small]['year'] = $news[ 'n_updatedAt' ]->format('Y');
                $news_list_small_highlight[$index_small]['month'] = $news[ 'n_updatedAt' ]->format('m');
            }
            $i++;
        }

        //$news_list_small_highlight = array_merge($news_list_small_highlight, $news_list_small_highlight); // TODO To delete

        return $this->render('BaseNewsBundle:Default:home.html.twig', array('news_list_big_highlight' => $news_list_big_highlight,
                                                                            'news_list_small_highlight' => $news_list_small_highlight));
    }

    /**
     * Generate the news feed
     *
     * @return Response XML Feed
     */
    public function feedAction() {
        $news_list = $this->getDoctrine()->getRepository('BaseNewsBundle:News')->findAll();

        $feed = $this->get('eko_feed.feed.manager')->get('article');
        $feed->addFromArray( $news_list );

        return new Response($feed->render('rss')); // or 'atom'
    }
}

?>